import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.net.URLEncoder;



@WebServlet("/a")
public class CookieServeltA extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("username", "root");
        cookie.setMaxAge(10);
        resp.addCookie(cookie);//设置cookie

        String username = "李 四";
        String encodedUsername = URLEncoder.encode(username, "UTF-8");//编码
        Cookie cookie1 = new Cookie("uname", encodedUsername);//设置cookie

        resp.addCookie(cookie1);
    }

}
